//! Copyright 2012 Virtium Technology, Inc.
//! All rights reserved
//!
#include "LedDriver.h"
#include "BoardHardware.h"
#include "Counter.h"

#if defined (__FW_BUILD__)
extern "C"  {
#include "stm32f10x.h"
}
#endif

cSysTick cLedDriver::s_LedTick;


void cLedDriver::InitReset()
{
#if defined (__FW_BUILD__)
//    GPIO_InitTypeDef GPIO_InitStructure;
//
//    RCC_APB2PeriphClockCmd(LED_PERIPH, ENABLE);
//    GPIO_InitStructure.GPIO_Pin = LED_PIN;
//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//    GPIO_Init(LED_PORT, &GPIO_InitStructure);
#endif
    //LED_OFF;
    s_LedTick.SetTimeout(1000);
    s_LedTick.ResetTimeBase();
}

void cLedDriver::SetLed(bool on)
{
    if (true == on) LED_ON;
    else LED_OFF;
}

void cLedDriver::InvLed()
{
    LED_INV;
}


void cLedDriver::RunOne()
{
#if TEST_MODE
    static U16 cnt = 0;
    static U8  ledCnt = 0;
#endif
    if (true == s_LedTick.CheckTimeOut())
    {
        s_LedTick.ResetTimeBase();
#if TEST_MODE
        if (9999 <= cnt)
        {
            cnt = 0;
        }
        else
        {
            cnt += 1111;
        }
        cCounter::SetCounter(cnt);
        ++ledCnt;
        if (2 == ledCnt) cCounter::SetLedRed(true);
        else if (4 == ledCnt) cCounter::SetLedGreen(true);
        else if (6 <= ledCnt) ledCnt = 0;
#endif
        cCounter::UpdateCounter();
        //InvLed();
    }
}

